package com.yksj.healthtalk.ui.salon;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.InfoAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.views.PullToRefreshListView;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * @author Administrator
 *相同经历查找
 */
public class SalonSearchSecondaryMenuActivity extends BaseFragmentActivity implements OnItemClickListener,OnClickListener{
	private ListView mListView;
	private InfoAdapter adapter;
	String[] datas;
	private ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
	private String [] str;
	private String [] strCode;
	private String dataClickId;
	private String mTitleName;
	private ArrayList<String> strCodes = new ArrayList<String>();
	private int position ;
	private GroupInfoEntity cacheEntity;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		setContentView(R.layout.friend_search_aboutexperience);
		
		initWidget();
		initData();
	}
	
	private void initData() {
//		str = (String[]) getIntent().getExtras().get("data");
		Bundle b =  getIntent().getExtras();
		str = b.getStringArray("data");
//		mTitleName =b.getString("whichDataClick");
		mTitleName  = getString(R.string.salon_child_level);
		strCode = b.getStringArray("dataCode");
		dataClickId = b.getString("dataClickId");
				
		setData(str);
		adapter = new InfoAdapter(getBaseContext(), data, true);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);
		titleTextV.setText(mTitleName);
		
		//初始话数据
		cacheEntity = (GroupInfoEntity) getIntent().getExtras().get("mGroupInfoEntity");
		if (cacheEntity != null && cacheEntity.getInfoLayName() != null && !cacheEntity.getInfoLayName().equals("") ) {
			for (int i = 0; i < str.length; i++) {
				if (cacheEntity.getInfoLayName().equals(str[i])) {
					this.position = i;
					adapter.removeSelect(position);
					adapter.addSelect(position);
					adapter.notifyDataSetChanged();
				}
			}
			;
		}
	}
	
	private void setData(String[] strs) {
		// TODO Auto-generated method stub
		data.clear();
		for (int i = 0; i < strs.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", i);
			map.put("title", strs[i]);
			map.put("content", "");
			data.add(map);
		}
	}

	/**
	 * 
	 */
	private void initWidget() {
		// TODO Auto-generated method stub
		initTitle();
		PullToRefreshListView pullRefreshView = (PullToRefreshListView) findViewById(R.id.att_list);
		pullRefreshView.setLayoutInvisible();
		mListView = pullRefreshView.getRefreshableView();
		
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText(R.string.sure);
	}

	/**
	 * listView 点击监听
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long arg3) {
		ImageView image = (ImageView) view.findViewById(R.id.att_icon);
		if(image.isShown()){
			image.setVisibility(View.GONE);
			adapter.removeSelect(position);
		}else{
			image.setVisibility(View.VISIBLE);
			adapter.removeSelect(position);
			adapter.addSelect(position);
		}
			this.position =  position;
		    adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			setResult(-1);
			finish();
			
			break;
		case R.id.title_right2:
			if (adapter.getSelects().size() == 0) {
				ToastUtil.showShort(this, "对不起,你还没有选择!");
			}else {
				Intent intent = new Intent(SalonSearchSecondaryMenuActivity.this,SalonSearchMainActivity.class);
				for (int i = 0; i < adapter.getSelects().size(); i++) {
					strCodes.add(strCode[adapter.getSelects().get(i)]);
				}
				intent.putExtra("positions", strCodes);
				intent.putExtra("dataClickId",dataClickId);
				intent.putExtra("position", position);
				setResult(0,intent);
				finish();
				;
			}
			
			break;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			setResult(-1);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

}
